﻿// LoginDialog.cpp: 实现文件
//

#include "pch.h"
#include "SP.h"
#include "LoginDialog.h"
#include "afxdialogex.h"


// LoginDialog 对话框

IMPLEMENT_DYNAMIC(LoginDialog, CDialogEx)

LoginDialog::LoginDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LOGIN, pParent)
	, m_cUserName(_T(""))
	, m_cPassward(_T(""))
	, User(_T(""))
	, Pallent(_T(""))
	, Program(_T(""))
	, LuosiMax(0)
{

}

LoginDialog::~LoginDialog()
{
}

void LoginDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);

	DDX_Text(pDX, IDC_EDIT_USERNAME, m_cUserName);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_cPassward);
	DDX_Text(pDX, IDC_EDIT1, User);
	DDX_Text(pDX, IDC_EDIT2, Pallent);
	DDX_Text(pDX, IDC_EDIT3, Program);
	DDX_Text(pDX, IDC_EDIT4, LuosiMax);
}

BOOL LoginDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	CComboBox* combo_box = nullptr;
	combo_box = (CComboBox*)GetDlgItem(IDC_COMBO_MODE_CHOOSE);

	combo_box->InsertString(0, L"员工");
	combo_box->InsertString(1, L"管理员");
	combo_box->SetCurSel(0);

	m_cPasswordCorrect = Manager.m_sLoginInfo.m_cPassword;
	m_cUserNameCorrect = Manager.m_sLoginInfo.m_cUserName;


	ReadConfigIni();

	return TRUE;
}

void LoginDialog::OnCancel()
{
	CDialogEx::OnCancel();
}

void LoginDialog::EnableControls(BOOL nflag)
{
	CButton* btn = nullptr;
	btn = (CButton*)GetDlgItem(IDC_BUTTON2);
	btn->EnableWindow(nflag);
	CString str;

	CButton* btnLogin = nullptr;
	btnLogin = (CButton*)GetDlgItem(IDC_BUTTON1);
	btnLogin->EnableWindow(!nflag);


	CMenu* pMenu = GetMenu();
	CWnd* wnd = AfxGetMainWnd();
	pMenu = Manager.m_ptrMainUI->GetMenu();

	
	if (ModeFlag == 1)
	{
		if (nflag == 1)
		{
			pMenu->EnableMenuItem(ID_32777, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32778, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32779, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32782, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32780, MF_ENABLED);
			pMenu->EnableMenuItem(ID_32781, MF_ENABLED);
		}
		else
		{
			pMenu->EnableMenuItem(ID_32777, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32778, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32779, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32782, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32780, MF_DISABLED);
			pMenu->EnableMenuItem(ID_32781, MF_DISABLED);

		}
	}
}

void LoginDialog::ReadConfigIni()
{
	User = Manager.UserMsg;
	Pallent = Manager.PallentMsg;
	Program = Manager.ProgramMsg;

	LuosiMax = Manager.LuosiMax;
	UpdateData(FALSE);
}

void LoginDialog::SaveValueToConfig()
{
	UpdateData(TRUE);

	//gSystemMgr.UserMsg = User;

	Manager.PallentMsg = Pallent;
	Manager.ProgramMsg = Program;
	Manager.LuosiMax = LuosiMax;

	Manager.m_cLoginConfigIni.SaveConfigIni(L"Login", L"PallentMsg", Manager.PallentMsg);

	Manager.m_cLoginConfigIni.SaveConfigIni(L"Login", L"ProgramMsg", Manager.ProgramMsg);
	CString cstr;
	cstr.Format(L"%d", Manager.LuosiMax);
	Manager.m_cLoginConfigIni.SaveConfigIni(L"Login", L"LuosiMax", cstr);


	Manager.GetLog()->DisplayAndSaveLog(L"Login保存数据完成", LV_EVENT);
}


BEGIN_MESSAGE_MAP(LoginDialog, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &LoginDialog::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &LoginDialog::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON2, &LoginDialog::OnBnClickedButton2)
END_MESSAGE_MAP()


// LoginDialog 消息处理程序


void LoginDialog::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);

	CComboBox* combo_box = (CComboBox*)(GetDlgItem(IDC_COMBO_MODE_CHOOSE));
	auto cboSelcect = combo_box->GetCurSel();

	//if(m_cPasswordCorrect == L"")
	//{
	//	m_cPasswordCorrect = L"1";
	//}
	if (cboSelcect == 0)
	{
		if (m_cUserNameCorrect != L"")
		{
			//int i = _ttoi(m_cUserName);
			Manager.UserMsg = m_cUserName;
			CString cstr;
			cstr.Format(L"员工号:%s  员工登陆成功!", m_cUserName);
			Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
			OnCancel();

			EnableControls(TRUE);
		}
		else
		{
			Manager.GetLog()->DisplayAndSaveLog(L"请输入员工号!", LV_ERROR);
		}

	}
	if (cboSelcect == 1)
	{
		UpdateData(TRUE);

		if (m_cUserName == m_cUserNameCorrect /*!= L""*/ && m_cPassward == m_cPasswordCorrect)
		{
			Manager.UserMsg = m_cUserName;
			ModeFlag = 1;
			//int i = _ttoi(m_cUserName);
			CString cstr;
			cstr.Format(L"员工号:%s  管理员登陆成功!", m_cUserName);
			Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
			OnCancel();
			EnableControls(TRUE);
		}
		else
		{
			Manager.GetLog()->DisplayAndSaveLog(L"登陆失败!", LV_ERROR);
		}
	}
	m_cUserName = L"";
	m_cPassward = L"";
	UpdateData(FALSE);
}


void LoginDialog::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	SaveValueToConfig();
}


void LoginDialog::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	Manager.GetLog()->DisplayAndSaveLog(L"退出登录成功!", LV_EVENT);
	EnableControls(FALSE);
	ModeFlag = 0;
}
